class Spring2 extends egret.DisplayObjectContainer {
    private ball: Ball
    private spring: number = 0.1
    private targetX: number = 0
    private targetY: number = 0
    private friction: number = 0.95

    public constructor() {
        super()
        this.once(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this)
    }

    private onAddToStage(event: egret.Event) {
        this.targetX = this.stage.stageWidth / 2
        this.targetY = this.stage.stageHeight / 2
        this.ball = new Ball()
        this.addChild(this.ball)
        this.ball.x = 0
        this.ball.y = 0
        this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this)
    }

    private onEnterFrame(event: egret.Event) {
        let dx = this.targetX - this.ball.x
        let dy = this.targetY - this.ball.y
        let ax = dx * this.spring
        let ay = dy * this.spring
        this.ball.vx += ax
        this.ball.vy += ay
        this.ball.vx *= this.friction
        this.ball.vy *= this.friction
        this.ball.x += this.ball.vx
        this.ball.y += this.ball.vy
    }
}